import json

import requests
from redis import StrictRedis

from configs.get_config_info import env_config
from scripts.scripts_log import get_log
from utils.crypto import Crypt

redis_config = env_config["redis"]
# host = env_config.get('HOST')
# port = env_config.get('PORT')
host = "172.16.1.100"
port = 8080
my_crypt = Crypt()
password = my_crypt.decrypt(redis_config.get("password"))
redis_coon = StrictRedis(
    host=redis_config.get("host"), port=redis_config.get("port"), password=password
)

image_code_id = "4f498f9d-dd42-44d7-a3f9-1f40889d3540"
app_log = get_log("redis_pub_sub")


def delete_vm(range_uuid, courseware_id, chapter_id):
    try:
        url = f"http://{host}:{port}/v1/organizer/range/activeRange/delete"
        headers = {
            "content-type": "application/json",
            "Authorization": "Bearer " + get_token(),
        }
        data = {"range_uuid": range_uuid}
        response = requests.delete(url, json=data, headers=headers)
        if response.status_code == 200:
            change_status(range_uuid, headers, courseware_id, chapter_id)
        return response

    except Exception as e:
        app_log.error({"delete_vm_error": str(e)})


def open_vm(range_uuid, courseware_id, chapter_id, env_start_time, env_end_time):
    try:
        url = f"http://{host}:{port}/v1/organizer/courseware/status"
        headers = {
            "content-type": "application/json",
            "Authorization": "Bearer " + get_token(),
        }
        data = {
            "range_uuid": range_uuid,
            "courseware_uuid": courseware_id,
            "chapter_uuid": chapter_id,
            "env_start_time": env_start_time,
            "env_end_time": env_end_time,
            "env_status": 1,
        }
        response = requests.post(url, json=data, headers=headers)
        if response.status_code == 200:
            app_log.info({"msg": "open_vm_success"})
        return response

    except Exception as e:
        app_log.error({"open_vm_error": str(e)})


def get_token():
    try:
        token = None
        image_code = get_image_code()
        url = f"http://{host}:{port}/v1/api/login"
        data = {
            "username": "zhangsan",
            "password": "YWJjMTIzIUAj",
            "image_code": image_code,
            "image_code_id": image_code_id,
        }
        response = requests.post(url, json=data)
        if response.status_code == 200:
            content = json.loads(response.content)
            if "data" in content:
                token = content["data"].get("token")
        return token

    except Exception as e:
        app_log.error({"get_token_error": str(e)})


def get_image_code():
    try:
        url = "http://{}:{}/v1/api/image_code?code_id={}".format(
            host, port, image_code_id
        )
        response = requests.get(url)
        real_image_code = None
        if response.status_code == 200:
            real_image_code = redis_coon.get("ImageCode_" + image_code_id).decode()
        return real_image_code

    except Exception as e:
        app_log.error({"get_image_code_error": str(e)})


def change_status(range_uuid, headers, courseware_id, chapter_id):
    try:
        url = f"http://{host}:{port}/v1/organizer/courseware/status"
        data = {
            "courseware_uuid": courseware_id,
            "chapter_uuid": chapter_id,
            "range_uuid": range_uuid,
            "env_status": "0",
        }
        response = requests.post(url, json=data, headers=headers)
        if response.status_code == 200:
            app_log.info({"msg": "change_env_status_success"})
        else:
            app_log.error({"msg": "change_env_status_error"})
    except Exception as e:
        app_log.error({"change_env_status_error": str(e)})
